package com.vkmessenger.gui.activities.contacts;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ScrollView;
import com.vkmessenger.R;
import com.vkmessenger.net.json.FriendsResponse;

/**
 * Created by IntelliJ IDEA.
 * User: Evgeniy Krasko
 * Date: 22.06.12
 * Time: 2:22
 */
public class MyScrollView extends ScrollView {

    public MyScrollView(Context context) {
        this(context, null);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Resources resources = getResources();

        separator = resources.getDrawable(R.drawable.silver_divider);
        separatorHeight = separator.getIntrinsicHeight();

        photo = resources.getDrawable(R.drawable.contact_nophoto);
        photoHeight = photo.getIntrinsicHeight();

        isOnline = resources.getDrawable(R.drawable.online_list);
        isOnlineHeight = isOnline.getIntrinsicHeight();

        grayPaint = new Paint();
        grayPaint.setTextAlign(Paint.Align.CENTER);
        grayPaint.setTypeface(Typeface.DEFAULT_BOLD);
        grayPaint.setColor(Color.GRAY);

        blackPaint = new Paint();
        blackPaint.setTextAlign(Paint.Align.LEFT);
        blackPaint.setTypeface(Typeface.DEFAULT_BOLD);
        blackPaint.setColor(Color.BLACK);

        separatorLetterHeight = grayPaint.getFontMetrics().ascent;
        photoLetterHeight = blackPaint.getFontMetrics().ascent;
    }


    private Drawable separator, photo, isOnline;
    private int separatorHeight, photoHeight, isOnlineHeight;
    private float separatorLetterHeight, photoLetterHeight;

    private Paint grayPaint, blackPaint;
    private Rect tmpRect = new Rect();

    private CommonContactsActivity parent = null;

    public void setParent(CommonContactsActivity parent) {
        this.parent = parent;
    }

    @Override
    public void onDraw(Canvas canvas) {
        if (parent != null) {
            parent.drawFriends(this, canvas);
        }
    }

    public int drawFriends(Canvas canvas, int h,
                           FriendsResponse.FriendResponse[] friends,
                           boolean useSeparator, boolean drawOnline) {
        char c = 0;
        for (FriendsResponse.FriendResponse friend : friends) {
            if (useSeparator) {
                char frC = friend.getFirst();
                if (c == 0 || frC != c) {
                    h = drawSeparator(canvas, h, frC);
                    c = frC;
                }
            }
            h = drawFriend(canvas, friend, h, drawOnline);
        }
        return h;
    }

    private int drawSeparator(Canvas canvas, int h, char frC) {
        canvas.getClipBounds(tmpRect);
        if (h + separatorHeight < tmpRect.top || h > tmpRect.bottom) {
            return h + separatorHeight;
        }

        separator.setBounds(0, h, canvas.getWidth(), h + separatorHeight);
        separator.draw(canvas);

        canvas.drawText(String.valueOf(frC), separatorHeight, h + (separatorHeight - separatorLetterHeight) / 2, grayPaint);
        return h + separatorHeight;
    }


    private int drawFriend(Canvas canvas, FriendsResponse.FriendResponse friend, int h, boolean drawOnline) {
        int padding = photoHeight / 10;
        h += padding;

        canvas.getClipBounds(tmpRect);
        if (h + photoHeight < tmpRect.top || h > tmpRect.bottom) {
            return h + photoHeight + padding;
        }

        photo.setBounds(padding, h, padding + photoHeight, h + photoHeight);
        photo.draw(canvas);

        canvas.drawText(friend.getTitle(), photoHeight + 2 * padding, h + (photoHeight - photoLetterHeight) / 2, blackPaint);

        if (drawOnline && "1".equals(friend.online)) {
            isOnline.setBounds(canvas.getWidth() - padding - isOnlineHeight,
                    h + (photoHeight - isOnlineHeight) / 2,
                    canvas.getWidth() - padding,
                    h + (photoHeight + isOnlineHeight) / 2);
            isOnline.draw(canvas);
        }
        return h + photoHeight + padding;
    }
}
